﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace XieCheng
{
    class CoroutineManager
    {
        private static CoroutineManager _instance = null;
        public static CoroutineManager Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new CoroutineManager();
                }
                return _instance;
            }
        }

        private LinkedList<IEnumerator> coroutineList = new LinkedList<IEnumerator>();

        public void StartCoroutine(IEnumerator ie)
        {
            coroutineList.AddLast(ie);
        }

        public void StopCoroutine(IEnumerator ie)
        {
            try
            {
                coroutineList.Remove(ie);
            }
            catch (Exception e) { Console.WriteLine(e.ToString()); }
        }

        public void UpdateCoroutine()
        {
            var node = coroutineList.First;
            while (node != null)
            {
                IEnumerator ie = node.Value;
                bool ret = true;
                if (ie.Current is IWait)
                {
                    IWait wait = (IWait)ie.Current;
                    //检测等待条件，条件满足，跳到迭代器的下一元素 （IEnumerator方法里的下一个yield）
                    if (wait.Tick())
                    {
                        ret = ie.MoveNext();
                    }
                }
                else
                {
                    ret = ie.MoveNext();
                }
                //迭代器没有下一个元素了，删除迭代器（IEnumerator方法执行结束）
                if (!ret)
                {
                    coroutineList.Remove(node);
                }
                //下一个迭代器
                node = node.Next;
            }
        }
    }
}
/// <summary>
/// 等待接口
/// </summary>
public interface IWait
{
    /// <summary>
    /// 每帧检测是否等待结束
    /// </summary>
    /// <returns></returns>
    bool Tick();
}
public class Time
{
    //每帧时间(秒)
    public static float deltaTime
    { get { return (float)deltaMilliseconds / 1000; } }
    //每帧时间（毫秒）
    public static int deltaMilliseconds
    { get { return 20; } }
}
/// <summary>
/// 按秒等待
/// </summary>
public class WaitForSeconds : IWait
{
    float _seconds = 0f;

    public WaitForSeconds(float seconds)
    {
        _seconds = seconds;
    }

    public bool Tick()
    {
        _seconds -= Time.deltaTime;
        return _seconds <= 0;
    }
}
/// <summary>
/// 按帧等待
/// </summary>
public class WaitForFrames : IWait
{
    private int _frames = 0;
    public WaitForFrames(int frames)
    {
        _frames = frames;
    }

    public bool Tick()
    {
        _frames -= 1;
        return _frames <= 0;
    }
}
